/**
 * Created by Administrator on 2016/12/20.
 */

$(function(){
    /*解析href*/
    var str = location.search;
    str = str.slice(1).split('&');
    var json = {};
    for (var i = 0; i < str.length; i++) {
        var a = str[i].split('=');
        json[a[0]] = a[1];
    }
    //console.log(json);

    /*工人出勤明细获取*/
    var
        oDataTime           = $('#dataTime'),//日期
        oOrgWorkTimeStart   = $('#orgWorkTimeStart'),//进场时间/工地
        oOrgWorkTimeEnd     = $('#orgWorkTimeEnd'),//出场时间/工地
        oWorkTime           = $('#workTime'),//工数/工地
        oChgWorkTimeStart   = $('#chgWorkTimeStart'),//进场时间/调整
        oChgWorkTimeEnd     = $('#chgWorkTimeEnd'),//出场时间/调整
        oChangeReason       = $('#changeReason'),//调整原因
        oChgWorkTimeAdjust  = $('#chgWorkTimeAdjust'),//调整工数
        oUnitSalary         = $('#unitSalary'),//工价/元
        oActualSalary       = $('#actualSalary');//实际金额/元

    var
        oDataTimeVal = '',
        oOrgWorkTimeStartVal = '',
        oOrgWorkTimeEndVal = '',
        oWorkTimeVal = '',
        oChgWorkTimeStartVal ='',
        oChgWorkTimeEndVal = '',
        oChangeReasonVal = '',
        oChgWorkTimeAdjustVal = '',
        oUnitSalaryVal = '',
        oActualSalaryVal = '';

    if(json.edit == 1){
        oDataTime.val(json.dataTime);
        oOrgWorkTimeStart.val(json.chgWorkTimeStart);
        oOrgWorkTimeEnd.val(json.chgWorkTimeEnd);
        oWorkTime.val(json.workTime);
        oChgWorkTimeStart.val(json.chgWorkTimeStart);
        oChgWorkTimeEnd.val(json.chgWorkTimeEnd);
        oChangeReason.val(decodeURI(json.changeReason));
        oChgWorkTimeAdjust.val();
        oUnitSalary.val(json.unitSalary);
        oActualSalary.val();
    }

    $('#saveBtn').click(function(){
            oDataTimeVal            =   oDataTime.val(),
            oOrgWorkTimeStartVal    =   oOrgWorkTimeStart.val(),
            oOrgWorkTimeEndVal      =   oOrgWorkTimeEnd.val(),
            oWorkTimeVal            =   oWorkTime.val(),
            oChgWorkTimeStartVal    =   oChgWorkTimeStart.val(),
            oChgWorkTimeEndVal      =   oChgWorkTimeEnd.val(),
            oChangeReasonVal        =   oChangeReason.val(),
            oChgWorkTimeAdjustVal   =   oChgWorkTimeAdjust.val(),
            oUnitSalaryVal          =   oUnitSalary.val(),
            oActualSalaryVal        =   oActualSalary.val();

        if(json.edit == 1){
            oData = [{
                "changeReason": oChangeReasonVal,
                "chgWorkTimeEnd": oChgWorkTimeEndVal,
                "chgWorkTimeStart": oChgWorkTimeStartVal,
                "dataTime": oDataTimeVal,
                "projectCode": json.projectCode,
                "teamId": json.teamId,
                "unitSalary": oUnitSalaryVal,
                "userCode": json.userCode
            }];

            $.ajax({
                type : 'POST',
                data: JSON.stringify(oData),
                url : 'http://192.168.59.3:8082/console/projectmgt/attendance/edit',
                dataType : 'json',
                contentType : 'application/json',
                success : function (data){
                    parent.layer.closeAll();
                },
                error : function (){
                }
            })
        }else{

            oData = [{
                "dataTime"          : oDataTimeVal,
                "orgWorkTimeStart"  : oOrgWorkTimeStartVal,
                "orgWorkTimeEnd"    : oOrgWorkTimeEndVal,
                "workTime"          : oWorkTimeVal,
                "unitSalary"        : oUnitSalaryVal,
                "projectCode"       : json.projectCode,
                "teamId"            : json.teamId,
                "userCode"          : json.userCode
            }];


            $.ajax({
                type : 'POST',
                data: JSON.stringify(oData),
                url : 'http://192.168.59.3:8082/console/projectmgt/attendance/add',
                dataType : 'json',
                contentType : 'application/json',

                success : function (data){
                    parent.layer.closeAll();
                },
                error : function (){
                }
            })
        }


    });

});

function responseHandler(res) {
    var needData = {
        rows: res.data.list,
        total: res.data.total
    };
    return needData;
}
